2020-2021 Sunseeker Telemetry and Lighting System
timer_d_ex6_hiResRegulatedPWM.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430F51x2 Demo - TimerD0, Hi-Res Regulated Mode, PWM TD0.0-2, DCO SMCLK
34 //
35 // Description: This code example shows how to configure TimerD in Hi-Res
36 // regulated mode. The reference input clock freq (in this e.g. SMCLK=12MHz)
37 // is multiplied by a factor of 16 to generate a hi-res freq of InputClk x 16
38 // (in this e.g. 12MHz x 16 = 192Mhz. With TD0CCR0=128-1, TD0.0 outputs a
39 // square wave of freq 746kHz and TD0.1 and TD0.2 output PWM waveforms with
40 // dutycycles of 25% and 75% respectively.
41 //
42 // NOTE: For using multiply factor 16x in hi-res calibration mode, make sure
43 // input ref clock is in the following freq range: 8Mhz to 16MHz.
44 //
45 // ACLK=LFXT1=32kHz; SMCLK=MCLK=12MHz; TDCLK(Hi-Res)= SMCLK x 16 = 192MHz
46 //
47 // MSP430F51x2
48 // -----------------
49 // /|\| XIN|-
50 // | | | 32kHz
51 // --|RST XOUT|-
52 // | |
53 // | P1.6/TD0.0|--> CCR0 - 50% dutycycle; PWM period = (2 x 128)/(12MHz x 16)~ 1.34us = 746kHz
54 // | P1.7/TD0.1|--> CCR1 - 25% dutycycle; ON period ~ 32/(12MHz x 16)~ 167ns
55 // | P2.0/TD0.2|--> CCR2 - 75% dutycycle; ON period ~ 96/(12MHz x 16)~ 500ns
56 //
57 //******************************************************************************
58 #include "driverlib.h"
59 
60 void main(void)
61 {
62  WDT_A_hold(WDT_A_BASE);
63 
64  // Configure XT1
65  // Port select XT1
66  GPIO_setAsPeripheralModuleFunctionInputPin(
67  GPIO_PORT_PJ,
68  GPIO_PIN4 + GPIO_PIN5
69  );
70 
71  UCS_turnOnLFXT1 (UCS_XT1_DRIVE_3,
72  UCS_XCAP_3
73  );
74 
75  // Increase Vcore setting to level1 to support fsystem=12MHz
76  PMM_setVCore (PMMCOREV_1);
77 
78  // Initialize DCO to 12MHz
79  UCS_initFLLSettle(12000,
80  374);
81 
82  // Configure TimerD in Hi-Res Regulated Mode
83  Timer_D_initHighResGeneratorInRegulatedModeParam param = {0};
84  param.clockSource = TIMER_D_CLOCKSOURCE_SMCLK;
85  param.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
86  param.clockingMode = TIMER_D_CLOCKINGMODE_HIRES_LOCAL_CLOCK;
87  param.highResClockMultiplyFactor = TIMER_D_CLOCKSOURCE_DIVIDER_1;
88  param.highResClockDivider = TIMER_D_HIGHRES_CLK_MULTIPLY_FACTOR_16x;
89  Timer_D_initHighResGeneratorInRegulatedMode(TIMER_D0_BASE, &param);
90 
91  Timer_D_enableHighResInterrupt(TIMER_D0_BASE,
92  TIMER_D_HIGH_RES_FREQUENCY_LOCK
93  );
94 
95  // Configure TimerD in Hi-Res Regulated Mode
96  // TDCLK=SMCLK=12MHz=Hi-Res input clk select
97  // Configure the CCRx blocks
98  // Configure the CCRx blocks
99  Timer_D_initCompareModeParam initComp0Param = {0};
100  initComp0Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_0;
101  initComp0Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
102  initComp0Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE;
103  initComp0Param.compareValue = 128-1;
104  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp0Param);
105 
106  Timer_D_initCompareModeParam initComp1Param = {0};
107  initComp1Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_1;
108  initComp1Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
109  initComp1Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE_SET;
110  initComp1Param.compareValue = 32;
111  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp1Param);
112 
113  Timer_D_initCompareModeParam initComp2Param = {0};
114  initComp2Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_2;
115  initComp2Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
116  initComp2Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE_SET;
117  initComp2Param.compareValue = 96;
118  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp2Param);
119 
120  Timer_D_initUpModeParam initUpParam = {0};
121  initUpParam.clockSource = TIMER_D_CLOCKSOURCE_SMCLK;
122  initUpParam.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
123  initUpParam.clockingMode = TIMER_D_CLOCKINGMODE_HIRES_LOCAL_CLOCK;
124  initUpParam.timerPeriod = 128-1;
125  initUpParam.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
126  initUpParam.captureCompareInterruptEnable_CCR0_CCIE =
127  TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
128  initUpParam.timerClear = TIMER_D_DO_CLEAR;
129  Timer_D_initUpMode(TIMER_D0_BASE, &initUpParam);
130 
131  Timer_D_startCounter(TIMER_D0_BASE,
132  TIMER_D_UP_MODE
133  );
134 
135  __bis_SR_register(LPM0_bits + GIE); // Enter LPM0
136  __no_operation(); // For debugger
137 }
138 
139 // Timer0_D1 Interrupt Vector (TDIV) handler
140 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
141 #pragma vector=TIMER0_D1_VECTOR
142 __interrupt
143 #elif defined(__GNUC__)
144 __attribute__((interrupt(TIMER0_D1_VECTOR)))
145 #endif
146 void TIMER0_D1_ISR(void)
147 {
148  switch(__even_in_range(TD0IV,30))
149  {
150  case 0: break; // No interrupt
151  case 2: break; // CCR1 not used
152  case 4: break; // CCR2 not used
153  case 6: break; // reserved
154  case 8: break; // reserved
155  case 10: break; // reserved
156  case 12: break; // reserved
157  case 14: break;
158  case 16: break;
159  case 18: // Clock fail low
160  while(1); // Input ref clock freq too low; trap here
161  case 20: // Clock fail high
162  while(1); // Input ref clock freq too high; trap here
163  case 22: // Hi-res freq locked
164  // Hi-Res freq locked; now configure ports to output PWMs at TD0.0/1/2
165  // P1.6,7 option select
166  // P1.6,7 output
167  // P2.0 options select
168  // P2.0 output
169  GPIO_setAsPeripheralModuleFunctionOutputPin(
170  GPIO_PORT_P1,
171  GPIO_PIN6 + GPIO_PIN7
172  );
173 
174  GPIO_setAsPeripheralModuleFunctionOutputPin(
175  GPIO_PORT_P2,
176  GPIO_PIN0
177  );
178  break;
179  case 24: break; // Hi-res freq unlocked
180  case 26: break; // reserved
181  case 28: break; // reserved
182  case 30: break; // reserved
183  default: break;
184  }
185 }
186 
187 
MPU_initThreeSegmentsParam param
__no_operation()
void main(void)
void TIMER0_D1_ISR(void)